package com.txtdhis.consumer.manage.controller;


import com.txtdhis.common.domain.User;
import com.txtdhis.common.domain.page.PageRequest;
import com.txtdhis.common.result.MsgUtil;
import com.txtdhis.common.result.Result;
import com.txtdhis.common.result.ResultUtil;
import com.txtdhis.consumer.manage.service.MqService;
import com.txtdhis.consumer.manage.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/manage")
@Slf4j
@RestController
@Api(tags = "用户管理相关接口")
public class UserController {


    @Autowired
    private UserService userservice;

    @Autowired
    private MqService mqservice;


/*
* 查询用户列表
* 参数user中可增加条件
* */
    @ApiOperation("按条件查询用户")
    @PostMapping("/queryUserForList")
    public Result<List<User>> queryUserForList(@RequestBody User user){
        log.info("开始查询1");
        List<User> userList = userservice.queryUserForList(user);
        return ResultUtil.getSuccess(MsgUtil.SELECT_SUCCESS,userList);

    }

    /*
     * 分页查询用户查询用户列表
     * 参数PageRequest
     * */
    @ApiOperation("分页查询用户")
    @PostMapping(value="/findPage")
    public Object findPage(@RequestBody PageRequest pageQuery) {
        return userservice.findPage(pageQuery);
    }


    /*
    * 按id查询user
    * */
    @ApiOperation("根据id查询用户")
    @GetMapping("/getUser/{id}")
    public Result<User> getUser(@PathVariable("id") String id){

        User user = userservice.getUser(Integer.valueOf(id));
        return  ResultUtil.getSuccess(MsgUtil.SELECT_SUCCESS, user);
    }


/*
* 新增user
* user中给id赋值就是修改
* */
    @ApiOperation("插入或修改新用户")
    @PostMapping("/insert")
    public Result insert(@RequestBody User user){

        int flag = userservice.insert(user);

       if(flag == 1)
           return  ResultUtil.getSuccess(MsgUtil.ADD_SUCCESS,true);
        else
           return  ResultUtil.getError(MsgUtil.ADD_FAIL,false);

    }

    @ApiOperation("插入redis缓存")
    @PostMapping("/setCache/{val}")
    public String setCache(@PathVariable("val") String val){
        return userservice.setCache(val);
    }


    //测试mq发送
    @ApiOperation("直连mq测试接口")
    @PostMapping("/sendDirectMessage")
    public String sendDirectMessage(){
        mqservice.sendDirectMessage();
        return "ok";
    }
    @ApiOperation("Topic类型mq测试接口1")
    @PostMapping("/sendTopicMessageMan")
    public String sendTopicMessageMan(){
        mqservice.sendTopicMessageMan();
        return "ok";
    }
    @ApiOperation("Topic类型mq测试接口1")
    @PostMapping("/sendTopicMessageWoman")
    public String sendTopicMessageWoman(){
        mqservice.sendTopicMessageWoman();
        return "ok";
    }

}
